package controller

import (
	"fmt"
	"gitee.com/saxon134/go-utils/saData"
	"gitee.com/saxon134/go-utils/saData/saTime"
	"gitee.com/saxon134/workflow/api"
	"gitee.com/saxon134/workflow/db"
	"gitee.com/saxon134/workflow/db/qa"
	"gitee.com/saxon134/workflow/db/user"
	"gitee.com/saxon134/workflow/errs"
	"gitee.com/saxon134/workflow/interface/ioBug"
	"strings"
)

func BugList(c *api.Context) (resp *api.Response, err error) {
	var in = new(ioBug.ListRequest)
	c.Bind(in)

	var ary = make([]*qa.TblBug, 0, c.Paging.Limit)
	var query = db.MySql.Table(qa.TBNBug).Order("status asc, p asc, id desc")

	var usrObj = new(user.TblUser)
	db.MySql.Table(user.TBNUser).Where("id = ?", c.Account.Id).First(usrObj)

	if in.DemandId > 0 {
		query.Where("demand_id = ?", in.DemandId)
	}

	if in.OnlyMe {
		query.Where("(creator = ? or partners like '%d')", c.Account.Id, c.Account.Id)
	} else {
		//管理员&经理之外，仅能查看自己的任务
		if strings.Contains(usrObj.Roles, "1") == false && strings.Contains(usrObj.Roles, "2") == false {
			query.Where("(creator = ? or partners like '%d')", c.Account.Id, c.Account.Id)
		}
	}

	if in.Status != 0 {
		query.Where("status = ?", in.Status)
	} else {
		query.Where("status in (1,2,3)")
	}

	if in.Keyword != "" {
		query.Where("title like '%%%s%%' or detail like '%%%s%%'", in.Keyword, in.Keyword)
	}

	if c.Paging.Total <= 0 {
		query.Count(&c.Paging.Total)
	}
	if c.Paging.Total <= int64(c.Paging.Offset) {
		return &api.Response{Result: nil}, nil
	}

	err = query.Find(&ary).Error
	if db.MySql.IsError(err) {
		return nil, errs.New(err)
	}

	return &api.Response{Result: ary}, nil
}

func BugSave(c *api.Context) (resp *api.Response, err error) {
	var in = new(ioBug.SaveRequest)
	c.Bind(in)

	if in.Content == "" || len(in.Users) == 0 {
		return nil, errs.New(errs.ErrorParams)
	}

	if in.Id <= 0 {
		in.Id = 0
		in.CreateAt = saTime.Now()
		in.Creator = c.Account.Id
		in.Status = 1
	}

	in.Partners = saData.AryToIds(in.Users, true)

	err = db.MySql.Save(in.TblBug).Error
	if err != nil {
		return nil, errs.New(err)
	}
	return &api.Response{Result: in}, nil
}

func BugUpdate(c *api.Context) (resp *api.Response, err error) {
	in := new(ioBug.UpdateRequest)
	c.Bind(in)
	if in.BugId <= 0 || in.Key == "" {
		return nil, errs.New(errs.ErrorParams)
	}

	if saData.InArray(in.Key, []string{"leader", "title", "detail", "status", "p", "deadline", "partners"}) == true {
		var sql = fmt.Sprintf("update qa_bug set %s = '%s' where id = %d;\n", in.Key, in.Value, in.BugId)
		err = db.MySql.Exec(sql).Error
		if err != nil {
			return nil, errs.New(err)
		}
		return &api.Response{Result: nil}, nil
	}

	if in.Key == "partners" {
		var idAry = saData.IdsToAry(in.Value)
		if len(idAry) <= 0 {
			return nil, errs.New(errs.ErrorParams)
		}

		err = db.MySql.Exec("update qa_bug set partners = ? where id = ?", saData.AryToIds(idAry, true), in.Value).Error
		if err != nil {
			return nil, errs.New(err)
		}

		return &api.Response{Result: nil}, nil
	}

	return nil, errs.New(errs.ErrorParams)
}
